module.exports = function ($resource, $state, $http, $q, toaster, $modal) {
	return {
		scope: {
            title: '=',
            data: '='
        },
        restrict: 'E',
        template: '<div></div>',
        replace: true,
		link: function (scope, element, attrs){
            var myChart = echarts.init(element[0]);

			var option = {
                title : {
                    text: scope.title,
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    top: '20px',
                    data: []    //数组内为空,不显示图例,要显示图例需要和series.data[i].name相对应
                },
                series : [
                    {
                        name: scope.title,
                        type: 'pie',
                        radius : '55%',
                        center: ['55%', '60%'],
                        data:[],
                        label : false
                    }
                ]
            };
            
            scope.$watch('data', function(newValue, oldValue){
                if(newValue){
                    for(var k in newValue){
                        option.series[0].data.push(newValue[k]);
                        option.legend.data.push(newValue[k].name);
                    }
                }
                myChart.setOption(option);
            }, true);

		}
	};
};

